'''
Created on 16 mars 2011

@author: wp803469

    FFT 1D: questions 3.1

'''


import numpy
import matplotlib.pylab as pylab
import Signaux.Signal as sig




def ploteur_sinusoide(N=30, Fs=440.0, A=1, ph=0, periods=100):
    '''
    pretty print de la sinusoide
    '''
    s = sig.Sinusoide(amplitude=A, phase=ph, frequence=Fs, samples=periods, n=N)
    f = (Fs/periods) * numpy.arange(-periods/2, periods/2)
    data = s.getValues()
    spectre = numpy.fft.fft(data[1])
    spectre = numpy.fft.fftshift(spectre)
    pylab.subplot(4,3,1)
    pylab.plot(data[0], data[1], color='brown', linestyle='-', marker='.')
    pylab.title('Sinusoide')
    pylab.xlabel('Temps (secondes)')
    pylab.ylabel('Amplitude')
    pylab.subplot(4,3,2)
    pylab.plot(f, abs(spectre), color='brown', linestyle='-', marker='.')
    pylab.xlabel('Frequences')
    pylab.ylabel('Magnitude (linear)')
    pylab.subplot(4,3,3)
    pylab.plot(f, numpy.angle(spectre), color='brown', linestyle='-', marker='.')
    pylab.xlabel('Frequences')
    pylab.ylabel('Phase (Radian)')
    
def ploteur_square(N=30, r=0.5, Fs=440.0, A=2, periods=100):
    '''
    pretty print du signal carre
    '''
    s = sig.Square(A, r, Fs, periods, N)
    f = (Fs/periods) * numpy.arange(-periods/2, periods/2)
    data = s.getValues()
    spectre = numpy.fft.fft(data[1])
    spectre = numpy.fft.fftshift(spectre)
    pylab.subplot(4,3,4)
    pylab.plot(data[0], data[1], '.-b')
    pylab.title('Square')
    pylab.xlabel('Temps (secondes)')
    pylab.ylabel('Amplitude')
    pylab.subplot(4,3,5)
    pylab.plot(f, abs(spectre), '.-b')
    pylab.xlabel('Frequences')
    pylab.ylabel('Magnitude (linear)')
    pylab.subplot(4,3,6)
    pylab.plot(f, numpy.angle(spectre), '.-b')
    pylab.xlabel('Frequences')
    pylab.ylabel('Phase (Radian)')

def ploteur_saw(N=30, Fs=440.0, A=1, periods=100):
    '''
    pretty print du signal en scie
    '''
    s = sig.Saw(A, Fs, periods, N)
    f = (Fs/periods) * numpy.arange(-periods/2, periods/2)
    data = s.getValues()
    spectre = numpy.fft.fft(data[1])
    spectre = numpy.fft.fftshift(spectre)
    pylab.subplot(4,3,7)
    pylab.plot(data[0], data[1], '.-r')
    pylab.title('Saw')
    pylab.xlabel('Temps (secondes)')
    pylab.ylabel('Amplitude')
    pylab.subplot(4,3,8)
    pylab.plot(f, abs(spectre), '.-r')
    pylab.xlabel('Frequences')
    pylab.ylabel('Magnitude (linear)')
    pylab.subplot(4,3,9)
    pylab.plot(f, numpy.angle(spectre), '.-r')
    pylab.xlabel('Frequences')
    pylab.ylabel('Phase (Radian)')

def ploteur_triangle(N=30, Fs=200.0, A=3, periods=100):
    '''
    pretty print du signal triangle
    '''
    s = sig.Triangle(A, Fs, periods, N)
    f = (Fs/periods) * numpy.arange(-periods/2, periods/2)
    data = s.getValues()
    spectre = numpy.fft.fft(data[1])
    spectre = numpy.fft.fftshift(spectre)
    pylab.subplot(4,3,10)
    pylab.plot(data[0], data[1], '.-g')
    pylab.title('Triangle')
    pylab.xlabel('Temps (secondes)')
    pylab.ylabel('Amplitude')
    pylab.subplot(4,3,11)
    pylab.plot(f, abs(spectre), '.-g')
    pylab.xlabel('Frequences')
    pylab.ylabel('Magnitude (linear)')
    pylab.subplot(4,3,12)
    pylab.plot(f, numpy.angle(spectre), '.-g')
    pylab.xlabel('Frequences')
    pylab.ylabel('Phase (Radian)')

    
def main():
    
    pylab.figure(figsize=(8, 8))
    pylab.subplots_adjust(wspace=.5, hspace=.5)
    
    ploteur_sinusoide()
    ploteur_square()
    ploteur_saw()
    ploteur_triangle()
    
    pylab.show()
    pass

if __name__ == '__main__':
    main()



